package com.weasel.weixin.mp.servlet;

import com.weasel.helper.DocumentConverter;
import com.weasel.helper.TimeHelper;
import com.weasel.weixin.commons.helper.SignatureHelper;
import com.weasel.weixin.commons.message.WeixinPostEventXmlMessageHandlerRouter;
import com.weasel.weixin.commons.message.bean.AbstractWeiXinPostCommonXmlAnswerMessage;
import com.weasel.weixin.commons.message.bean.WeixinPostEventXmlMessage;
import com.weasel.weixin.commons.message.builder.WeixinPostCommonXmlAnswerMessageBuilders;
import com.weasel.weixin.commons.message.handler.*;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Dylan on 2015/9/28.
 */
public class WeixinPostEventXmlMessageServlet extends WeixinBaseServlet {


    private WeixinPostEventXmlMessageHandlerRouter messageHandlerRouter = new WeixinPostEventXmlMessageHandlerRouter();

    {
        messageHandlerRouter.addHandler(new WeixinPostClickEventXmlMassageHandler() {
            @Override
            protected Object handler(WeixinPostEventXmlMessage message) {
                System.out.println("用户点击定义菜单拉取消息");
                return clickEventResponse(message);
            }
        }).addHandler(new WeixinPostLocationEventXmlMassageHandler() {
            @Override
            protected Object handler(WeixinPostEventXmlMessage message) {
                System.out.println("用户上传了地理位置");
                return null;
            }
        }).addHandler(new WeixinPostScanEventXmlMassageHandler() {
            @Override
            protected Object handler(WeixinPostEventXmlMessage message) {
                System.out.println("用户重复关注了公众号");
                return null;
            }
        }).addHandler(new WeixinPostUnSubscribeEventXmlMassageHandler() {
            @Override
            protected Object handler(WeixinPostEventXmlMessage message) {
                System.out.println("用户取消了关注公众号");
                return null;
            }
        }).addHandler(new WeixinPostSubscribeEventXmlMassageHandler() {
            @Override
            protected Object handler(WeixinPostEventXmlMessage message) {
                System.out.println("用户关注了公众号");
                return null;
            }
        }).addHandler(new WeixinPostViewEventXmlMassageHandler() {
            @Override
            protected Object handler(WeixinPostEventXmlMessage message){
                System.out.println("用户点击了类型为view的算定义菜单");
                return null;
            }
        });
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        WeixinPostEventXmlMessage message = WeixinPostEventXmlMessage.fromWeixinPostInputSteam(request.getInputStream());

        Object result = messageHandlerRouter.handler(message);
        response.getWriter().write(result.toString());

    }


    private String clickEventResponse(WeixinPostEventXmlMessage message){

        String bindUser = BindUserServlet.userBinder.getIfPresent(message.getFromUserName());
        String messageJson = "您当前积分为10";
        if(StringUtils.isEmpty(bindUser)){
            String timestamp = String.valueOf(TimeHelper.getAfterMinuteTime(1).getTime());
            String signature = SignatureHelper.create("mytoken",timestamp,message.getFromUserName());
            messageJson = "您好,请先验证身份,即可免费开通本项功能.\n<a href=\"http://yhwemall.ngrok.jlw365.com/bind_user?signature="+signature+"&timestamp="+timestamp+"&openid="+message.getFromUserName()+"\">授权认证</a>";
        }
        AbstractWeiXinPostCommonXmlAnswerMessage answerMessage = WeixinPostCommonXmlAnswerMessageBuilders.answerText()
                                                                                                         .from(message.getToUserName())
                                                                                                         .to(message.getFromUserName())
                                                                                                         .content(messageJson)
                                                                                                         .build();
        return DocumentConverter.toXML(answerMessage);
    }

}
